var serverData = {};
serverData.key ='';
var	serverTable;

var serverData1 = {};
serverData1.key="";
var serverTable1;

jQuery("#orderUnitArea").select2().trigger("change");

//引入客户信息弹窗	 
jQuery('#queryCustomer').on("click",function(){
	 jQuery("#queryCustomerModal").modal('show');
});
function selectOne(obj){
	if(obj.checked){
		$('.chances').prop('checked',false);
		obj.checked = true;
	}
}

jQuery('#btnCSave').on("click",function(){
	if (jQuery("#chanceForm") && !jQuery("#chanceForm").Validform({
		tiptype : 3,
		showAllError : true
	}).check(false)) {return;}
	var chanceId = $("#chanceId").val();
	if(chanceId == ''){
		alert('请选择主体信息!');
		return;
	}
	
	jQuery("#btnCSave").attr("disabled","disabled");
	var api = window.rootPath + "/ui/expectedOrder/save2chance";
	var d={
			"chanceId" : $("#chanceId").val(),
			"orderUnitArea" : $("#orderUnitArea").val(),
			"orderUnit" : $("#orderUnit").val(),
			"name" : $("#name").val(),
			"deviceState" : $("#deviceState").val(),
			"chargePerson" : $("#chargePerson").val(),
			"probability" : $("#probability").val(),
			"orderAmount" : $("#orderAmount").val(),
			"amountMoney" : $("#amountMoney").val(),
			"expectedDate" : $("#expectedDate").val(),
			"explain" : $("#explain").val()
	};
	jQuery.ajax({
		type : 'post',
		url : api,
		data : JSON.stringify(d),
		dataType : 'json',
		contentType : 'application/json;charset=utf-8',
		success : function(data) {
			jQuery("#btnCSave").removeAttr("disabled");
			window.location = window.rootPath + "/ui/expectedOrder";
		}
	});
});


jQuery('#pull-in').on("click",function(){
	 var selects=getSelect("#customerTable",5);
  	   if(selects.length>1){
  		   $.springLemonDialog({
  		         content: '只能操作一条记录',// 确认文本
  		         clickButton: function (sender, modal, index) {// 按钮事件处理
  		             $(this).closeDialog(modal);
  		         }
  		   }); 
  	   }
  	   else if(selects.length==0){
  		   $.springLemonDialog({
  		         content: '请选择要操作的记录',// 确认文本
  		         clickButton: function (sender, modal, index) {// 按钮事件处理
  		             $(this).closeDialog(modal);
  		         }
  		   }); 
  	   }else if(selects.length==1){
			jQuery("#orderUnit").val(selects[0].customerName);
			jQuery("#orderUnitArea").select2().val(selects[0].saleArea.superiorArea).trigger("change");
			jQuery("#queryCustomerModal").modal('hide');
  	   }
});


jQuery(document).ready(function () {
	serverTable =  jQuery('#dataTableOrder').DataTable({
	    "serverSide": true,
	    "ordering": false,
	    "ajax": {
	        "dataType": "json",
	        "type": "get",
	        "cache": false,
	        "url": window.rootPath+'/ui/expectedOrder/getpageChance',
	        "data": function (d) {
	            return $.extend({}, d, serverData);
	        }
	    },
	    columns: [
	        {
	        data: null,
	        defaultContent: ""
	        },{
		    data:'id',
		    render: function(data, type, full, meta){
		    	var operate ='<input type="checkbox" class="chances" data-id="'+data+'" onclick="selectOne(this)" >';
		    	var id=data.id;
		    	return operate;
						
				}
	       },
	        {
	        	data: 'name'
	        },
	        {
	        	data: 'chancePriority'
	        },
	        {
	        	data: 'business'
	        },
	        {
	            data: 'source'
	        },
	        {
	            data: 'sourceUnit'
	        },
	        {
	            data: 'user.name'
	        }
	       
	        ],
	    "drawCallback": function ( oSettings ) {             
	        var k = 0;
	        var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;
	
	        for ( var i=oSettings._iDisplayStart, iLen=total ; i<iLen ; i++ )
	        {
	            $('td:eq(0)', oSettings.aoData[ oSettings.aiDisplay[k] ].nTr ).text( i+1 );
	            k++;
	        }
	
	        serverTable.off('click','tr');
	        serverTable.on( 'click', 'tr', function (event) {
	            event=event||window.event;
	            var target=event.target||event.scrElement;
	            var data = serverTable.row( this ).data();
	
	            return;
	        } );
	    }
	    
	});
	
	//获取选择预计订单信息
	jQuery("#btnSelect").on('click',function(){
		var obj = $('.chances:checked');
		if(obj.length == 0){
			alert("请选择主信息!");
			return;
		}
		var chanceId = obj.eq(0).attr('data-id');
		 var url = window.rootPath+'/ui/expectedOrder/getChance/'+chanceId;
		 jQuery.ajax({
				url : encodeURI(url),
				type : 'post',
				data : '',
				dataType : 'json',
				contentType : 'application/json;charset=utf-8',
				success : function(data) {
					selectChance= data.chance;
					setChanceValue(selectChance);
				}
			}); 
	});
	
	function setChanceValue(data){
		//清空表单
		$('#chanceForm')[0].reset();
		
		$('#chanceId').val(data.id);
		$('#orderUnitArea').select2().val(data.orderUnitArea).trigger("change");
		$('#orderUnit').val(data.orderUnit);
		$('#name').val(data.name);
		$('#deviceState').val(data.deviceState);
	}
	
	jQuery('#btnReSet').on("click",function(){
		//清空表单
		$('#chanceForm')[0].reset();
//		if(selectChance.id != undefined){
//			setChanceValue(selectChance);
//		}
	});
	
	
  //查询
  jQuery('#searchBtn').on('click', function() {
		serverData.key = jQuery('#keyword').val();
		serverTable.draw();
	});
	// 撤销查询
	jQuery('#resetBtn').on("click", function() {
		jQuery('#keyword').val('');
		serverData.key = '';
		serverTable.draw();
	});
	
	
	 serverTable1 = jQuery('#customerTable').DataTable({
         "serverSide": true,
         "ordering": false,
         "ajax": {
             "dataType": "json",
             "type": "get",
             "cache": false,
             "url":window.rootPath+ '/ui/customer/getpage',
             "data": function (d) {
                 return $.extend({}, d, serverData1);
             }
         },
         "iDisplayLength": 5,
         columns: [
             {
             data: null
             },
             {
             	data: 'customerName'
             },
             {
                 data: 'saleArea.areaName'
             },
             {
                 data: 'business'
             },
             {
                 data: 'superior'
             },
             {
             	data: null,
                 defaultContent: "",
                 render: function (data, type, full, meta) {
                     var operate =//'<button type="button" class="btn btn-sm btn-info editBtn" title="编辑"><i class="fa fa-pencil"></i></button>'
                                    '<input type="checkbox" class="checkCustomer">';
                     return operate;

                 }
                 
             } 
           ],
         "drawCallback": function (oSettings) {
             var k = 0;
             var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

             for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                 $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                 k++;
             }

             serverTable1.off('click', 'tr');
             serverTable1.on('click', 'tr', function (event) {
                 event = event || window.event;
                 var target = event.target || event.scrElement;
                 var data = serverTable1.row(this).data();

                 return;
             });
         }
     });
 //搜索
	jQuery('#searchBtn1').on('click', function() {
		serverData1.key = jQuery('#keyword1').val();
		serverTable1.draw();
	})
	// 撤销查询
	jQuery('#resetBtn1').on("click", function() {
		jQuery('#keyword').val('');
		serverData1.key = '';
		serverTable1.draw();
	});
	
});